package cn.jiyun.controller;

import cn.jiyun.constant.MessageConstant;
import cn.jiyun.entity.PageResult;
import cn.jiyun.entity.QueryPageBean;
import cn.jiyun.entity.Result;
import cn.jiyun.pojo.Setmeal;
import cn.jiyun.serivce.SetmealService;
import cn.jiyun.utils.QiniuUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);

            Jedis jedis = jedisPool.getResource();
            jedis.sadd("setB",setmeal.getImg());

            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("findCheckItemIdsByGid")
    public Result findCheckItemIdsByGid(Integer id){
        try {
            List<Integer> ids = setmealService.findCheckItemIdsByGid(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile, HttpServletRequest request){
        try {

            String img = "";
            if(imgFile!=null && imgFile.getSize()>0){
                String path = request.getSession().getServletContext().getRealPath("/");
                imgFile.transferTo(new File(path+"/"+imgFile.getOriginalFilename()));

                img = "../"+imgFile.getOriginalFilename();
            }

            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,img);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("upload2")
    public Result upload2(MultipartFile imgFile){
        try {

            String fileName = "";


            if(imgFile!=null && imgFile.getSize()>0){
                String filename = imgFile.getOriginalFilename();
                fileName = UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
                QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            }

            Jedis jedis = jedisPool.getResource();
            jedis.sadd("setA",fileName);

            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
